package com.ygqh.baby.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgMobileMapper;
import com.ygqh.baby.po.YgMobile;
import com.ygqh.baby.po.YgMobileExample;
import com.ygqh.baby.service.YgMobileService;

@Service
public class YgMobileServiceImpl implements YgMobileService {

	@Autowired
	private YgMobileMapper ygMobileMapper;

	@Override
	public List<YgMobile> find() {
		YgMobileExample example = new YgMobileExample();
		return ygMobileMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgMobileExample example = new YgMobileExample();
		return ygMobileMapper.countByExample(example);
	}

	@Override
	public YgMobile findById(Long id) {
		return ygMobileMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgMobile ygMobile) {
		return ygMobileMapper.insertSelective(ygMobile);
	}

	@Override
	public int update(YgMobile ygMobile) {
		return ygMobileMapper.updateByPrimaryKeySelective(ygMobile);
	}

	@Override
	public ResultSet<YgMobile> search(QueryInfo queryInfo, String q, Date startTime, Date endTime) {
		List<YgMobile> list = ygMobileMapper.selectSuper(queryInfo, q, startTime, endTime);
		int count = ygMobileMapper.countSuper(q, startTime, endTime);
		ResultSet<YgMobile> resultSet = new ResultSet<YgMobile>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		return 0;
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgMobile ygMobile) {
		int r = 0;
		if (ygMobile.getId() != null) {
			r = this.update(ygMobile);
		} else {
			r = this.save(ygMobile);
		}
		return r;
	}

	@Override
	public int add(String telPhone, String content) {
		YgMobile ygMobile = new YgMobile();
		ygMobile.setMobile(Long.parseLong(telPhone));
		ygMobile.setContent(content);
		ygMobile.setCreateTime(new Date());
		return this.save(ygMobile);
	}

	@Override
	public int countByTime(Date startDate, Date endDate) {
		YgMobileExample example = new YgMobileExample();
		example.createCriteria().andCreateTimeBetween(startDate, endDate);
		return ygMobileMapper.countByExample(example);
	}

}
